﻿@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<script>
    var spaceId = $.request("spaceId");
    var F_SiteId = $.request("F_SiteId");
    var F_MarketPrice = $.request("F_MarketPrice");
    var F_FloorPrice = $.request("F_FloorPrice");
    var F_YunPrice = $.request("F_YunPrice");
    $(function () {
        var sc = document.getElementsByName("SC");
        for (var i = 0; i < sc.length; i++) {
            sc[i].value = F_MarketPrice;
        }
        var sc = document.getElementsByName("YUN");
        for (var i = 0; i < sc.length; i++) {
            sc[i].value = F_YunPrice;
        }
        var sc = document.getElementsByName("FP");
        for (var i = 0; i < sc.length; i++) {
            sc[i].value = F_FloorPrice;
        }
    });
    var stardate,enddate;
    function submitForm() {

        var queryJson = {
            F_SiteId: '',
            F_SpaceId: '',
            F_ScheduleDate: '',
            F_YunPrice: '',
            F_MarketPrice: '',
            F_FloorPrice: ''
        };
        var JsonList = [];
        stardate = document.getElementById("F_StartDate").value;
        enddate = document.getElementById("F_EndDate").value;
        var days = getDays(stardate, enddate);
        if (days > 0) {
            for (var i = 0; i < days + 1; i++) {
                var date = addDate(stringToDate(stardate), i);
                var week = getWeek(stringToDate(date).getDay())
                var y = document.getElementById("F_YUN_W" + week).value;
                var m = document.getElementById("F_SC_W" + week).value;
                var f = document.getElementById("F_DJ_W" + week).value
                if (y == "" && m == "" && f == "") {
                    continue;
                } else {
                    queryJson = {
                        F_SiteId: F_SiteId,
                        F_SpaceId: spaceId,
                        F_ScheduleDate: date,
                        F_YunPrice: y == "" ? 0 : y,
                        F_MarketPrice: m == "" ? 0 : m,
                        F_FloorPrice: y == "" ? 0 : y,
                    }
                    JsonList.push(queryJson);
                }
            }

            var params = {
                priceScheduleOutPut: JSON.stringify({
                    PriceScheduleList: JsonList,
                    sDate: document.getElementById("F_StartDate").value,
                    eDate: document.getElementById("F_EndDate").value
                })
            };
            $.ajax({
                url: "/SiteManage/PriceSchedule/SubmitForm",
                dataType: "json",
                type: 'post',
                data: params,
                async: false,
                success: function (data) {
                    $.modalAlert(stardate + "~" + enddate + "价格已提交成功！", "warning");
                }, error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest);
                    console.log(textStatus);
                    console.log(errorThrown);
                }
            });
        }
        else {
            $.modalAlert("开始时间不能大于结束时间！", "warning");
        }
    }
    //日期加减
    function addDate(date, days) {
        var d = new Date(date);
        d.setDate(d.getDate() + days);
        var m = d.getMonth() + 1;
        return d.getFullYear() + '-' + m + '-' + d.getDate();
    } 
    //日期转换
    function stringToDate(fDate) {
        var fullDate = fDate.split("-");

        return new Date(fullDate[0], fullDate[1] - 1, fullDate[2], 0, 0, 0);
    } 
    //获取日期为周几
    function getWeek(week) {
        var str1 = "";
        switch (week) {
            case 0:
                str1 = "7";
                break;
            case 1:
                str1 = "1";
                break;
            case 2:
                str1 = "2";
                break;
            case 3:
                str1 = "3";
                break;
            case 4:
                str1 = "4";
                break;
            case 5:
                str1 = "5";
                break;
            case 6:
                str1 = "6";
                break;
        }
        return str1;
    }
    //获取两个日期之间的天数
    function getDays(date1, date2) {
        var date1Str = date1.split("-");//将日期字符串分隔为数组,数组元素分别为年.月.日  
        //根据年 . 月 . 日的值创建Date对象  
        var date1Obj = new Date(date1Str[0], (date1Str[1] - 1), date1Str[2]);
        var date2Str = date2.split("-");
        var date2Obj = new Date(date2Str[0], (date2Str[1] - 1), date2Str[2]);
        var t1 = date1Obj.getTime();
        var t2 = date2Obj.getTime();
        var dateTime = 1000 * 60 * 60 * 24; //每一天的毫秒数  
        var minusDays = Math.floor(((t2 - t1) / dateTime));//计算出两个日期的天数差  
        var days = Math.abs(minusDays);//取绝对值  
        return days;
    }  

    function btn_PriceSchedule() {
            $.modalOpen({
                id: "schedule",
                title: "价格预览",
                url: "/SiteManage/Space/PriceSchedule?keyValue=" + spaceId + "&F_MarketPrice=" + F_MarketPrice,
                width: "760px",
                height: "700px",
                btn: null
            });
    }
</script>

<script src="~/Content/js/jqgrid/jqgrid.min.js"></script>
<script src="~/Content/js/jqgrid/grid.locale-cn.js"></script>
<link href="~/Content/js/jqgrid/jqgrid.css" rel="stylesheet" />
<form id="form1">
    <div style="padding-top: 20px; margin-right: 20px ;width: 760px;margin:auto;">
        <table class="form">
            <tr>
                <th class="formTitle">开始日期</th>
                <td class="formValue" colspan="2">
                    <input id="F_StartDate" name="F_StartDate" type="text" class="form-control input-wdatepicker" onfocus="WdatePicker({skin:'whyGreen',dateFmt: 'yyyy-MM-dd', minDate:'%y-%M-{%d+1}', readOnly:true})" />
                </td>
                <th class="formTitle">结束日期</th>
                <td class="formValue" colspan="2">
                    <input id="F_EndDate" name="F_EndDate" type="text" class="form-control input-wdatepicker" onfocus="WdatePicker({skin:'whyGreen',dateFmt: 'yyyy-MM-dd', minDate:'%y-%M-{%d+1}', readOnly:true})" />
                </td>
                <td>
                    <a id="NF-schedule" authorize="yes" class="btn btn-primary dropdown-text" onclick="btn_PriceSchedule()"><i class="fa fa-pencil-square-o"></i>价格预览</a>
                </td>
                <td></td>
            </tr>
            <tr style="text-align:center;height:40px; line-height:40px;">
                <th class="formTitle"><b>星期</b></th>
                <th class="formTitle" colspan="2"><b>市场价格</b></th>
                <th class="formTitle" colspan="2"><b>YUNSPACE价格</b></th>
                <th class="formTitle" colspan="2"><b>场地底价</b></th>
            </tr>
            <tr>
                <th class="formTitle">星期一</th>
                <td class="formValue" colspan="2">
                    <input id="F_SC_W1" name="SC" type="number" class="form-control" />
                </td>
                <td class="formValue" colspan="2">
                    <input id="F_YUN_W1" name="YUN" type="number" class="form-control" />
                </td>
                <td class="formValue" colspan="2">
                    <input id="F_DJ_W1" name="FP" type="number" class="form-control" />
                </td>
            </tr>
            <tr>
                <th class="formTitle">星期二</th>
                <td class="formValue" colspan="2">
                    <input id="F_SC_W2" name="SC" type="number" class="form-control" />
                </td>
                <td class="formValue" colspan="2">
                    <input id="F_YUN_W2" name="YUN" type="number" class="form-control" />
                </td>
                <td class="formValue" colspan="2">
                    <input id="F_DJ_W2" name="FP" type="number" class="form-control" />
                </td>
            </tr>
            <tr>
                <th class="formTitle">星期三</th>
                <td class="formValue" colspan="2">
                    <input id="F_SC_W3" name="SC" type="number" class="form-control" />
                </td>
                <td class="formValue" colspan="2">
                    <input id="F_YUN_W3" name="YUN" type="number" class="form-control" />
                </td>
                <td class="formValue" colspan="2">
                    <input id="F_DJ_W3" name="FP" type="number" class="form-control" />
                </td>
            </tr>
            <tr>
                <th class="formTitle">星期四</th>
                <td class="formValue" colspan="2">
                    <input id="F_SC_W4" name="SC" type="number" class="form-control" />
                </td>
                <td class="formValue" colspan="2">
                    <input id="F_YUN_W4" name="YUN" type="number" class="form-control" />
                </td>
                <td class="formValue" colspan="2">
                    <input id="F_DJ_W4" name="FP" type="number" class="form-control" />
                </td>
            </tr>
            <tr>
                <th class="formTitle">星期五</th>
                <td class="formValue" colspan="2">
                    <input id="F_SC_W5" name="SC" type="number" class="form-control" />
                </td>
                <td class="formValue" colspan="2">
                    <input id="F_YUN_W5" name="YUN" type="number" class="form-control" />
                </td>
                <td class="formValue" colspan="2">
                    <input id="F_DJ_W5" name="FP" type="number" class="form-control" />
                </td>
            </tr>
            <tr>
                <th class="formTitle">星期六</th>
                <td class="formValue" colspan="2">
                    <input id="F_SC_W6" name="SC" type="number" class="form-control" />
                </td>
                <td class="formValue" colspan="2">
                    <input id="F_YUN_W6" name="YUN" type="number" class="form-control" />
                </td>
                <td class="formValue" colspan="2">
                    <input id="F_DJ_W6" name="FP" type="number" class="form-control" />
                </td>
            </tr>
            <tr>
                <th class="formTitle">星期日</th>
                <td class="formValue" colspan="2">
                    <input id="F_SC_W7" name="SC" type="number" class="form-control" />
                </td>
                <td class="formValue" colspan="2">
                    <input id="F_YUN_W7" name="YUN" type="number" class="form-control" />
                </td>
                <td class="formValue" colspan="2">
                    <input id="F_DJ_W7" name="FP" type="number" class="form-control" />
                </td>
            </tr>
        </table>
    </div>
</form>

